import {Res, Student} from "../types";
import {deleteRequest, error, getRequest, ok, postRequest, putRequest} from "../utils/myAxios.ts";
export async function getStuList(query?:string): Res<Array<Student>> {
    let str = query ? `?&name_like=${query}` : '';
    const stuList = await getRequest<Array<Student>>(`/student` + str);
    console.log('获取学生列表',stuList)
    //判断loginStu的id属性是否存在
    if (stuList?.length >= 0 ) {
        return {
            code: 200,
            data: stuList,
            flag: true,
            msg: "ok"
        }
    } else {
        return {
            code: 400,
            data: [],
            flag: false,
            msg: "请求失败"
        }
    }

}
export async function getStuListByClassroomId(classId?:string): Res<Array<Student>> {
    let str = classId ? `?&classId=${classId}` : '';
    const stuList = await getRequest<Array<Student>>(`/student` + str);
    console.log('获取学生列表',stuList)
    //判断loginStu的id属性是否存在
    if (stuList?.length >= 0 ) {
        return {
            code: 200,
            data: stuList,
            flag: true,
            msg: "ok"
        }
    } else {
        return {
            code: 400,
            data: [],
            flag: false,
            msg: "请求失败"
        }
    }
}
export async function addStudent( student:Student) : Res<Student>{
    const r = await postRequest<Student>(`/student`,student);
    console.log('添加学生：',r)
    if(r.id){
        return ok(r,'添加成功');
    }else {
        return error(r)
    }
}

export async function updateStudent( student:Student) : Res<Student>{
    const r = await putRequest<Student>(`/student/${student.id}`,student);
    console.log('修改学生res：',r)
    if(r.id){
        return ok(r,'修改成功');
    }else {
        return error(r)
    }
}

export async function deleteStudent( id:number) : Res<Student>{
    const r = await deleteRequest<Student>(`/student/${id}`);
    console.log('删除学生res：',r)
    if(r.id){
        return ok(r,'删除成功');
    }else {
        return error(r)
    }
}
//跟据id查询
export async function getStudentById(id:number): Res<Student>{
    const r = await getRequest<Student>(`/student/${id}`);
    console.log('查询学生res：',r)
    if(r.id){
        return ok(r,'查询成功');
    }else {
        return error(r)
    }
}

export async function getStudentByAccount(account:string): Res<Array<Student>>{
    const r = await getRequest<Array<Student>>(`/student?account=${account}`);
    if(r.length >= 0){
        return ok(r,'查询成功');
    }else {
        return error(r)
    }
}